import requests
from parsel import Selector
import csv
from 获取二手房房源信息页面列表 import main1
import time

COUNT = 1

class GetHouseDetail:
    def __init__(self, url, csv_writer):
        self.url = url
        self.csv_writer = csv_writer
        self.cookies = {
            'SECKEY_ABVK': 'yN6sMd5ARVvSUB7X+vkXdWvn5eBpPhXdn13hkWGVZ/0%3D',
            'BMAP_SECKEY': 'MXqX4OT-tHaH65jB1haMsbqW-qgBjmhRBGAY4WPuNkO3j4clIyRZde0H_7fzB-3tx98O28elBQiFIVd3E_-gHdB7123inHZzisq8NDx5Ps2ybuiHSrn56rQmrChwOabBO6WF8qlnruS9zHqTO2p5RvEZK9-bxK0ZitgaikWZ7PcAfmhYlPGHsUs4iG59AERB',
            'aQQ_ajkguid': '257EB14C-D5FC-4A0C-AE4C-B29EE0023134',
            'sessid': 'ED113258-2FF4-4CD7-B0E7-E168D210D662',
            'ajk-appVersion': '',
            'ctid': '27',
            'id58': 'CrIcp2WZmzZrrx8uRURFAg==',
            'isp': 'true',
            'xxzlcid': '5ac0f643503149f1a01135e6b55d55c6',
            'xxzl-cid': '5ac0f643503149f1a01135e6b55d55c6',
            'fzq_h': 'cd7ce9745e16db1bc94687924442373c_1704737951984_95d6e2b691194383a49fadab5eb18bdf_3740786969',
            'twe': '2',
            'fzq_js_anjuke_ershoufang_pc': 'f39cf68ef60c18fcb3f2cf75b2d55029_1704781910567_25',
            'obtain_by': '2',
            'ajk_member_verify': 'nGamCrFRDk0vJp%2FMiRoVQPDhLMdc%2FwFJFQTH9x1D0A8%3D',
            'ajk_member_verify2': 'MjUxNzgyMzkxfEw3YlJ1Rkd8MQ%3D%3D',
            '58tj_uuid': '86ff780a-1d02-4f6c-a3cd-f9f096505666',
            'new_session': '1',
            'init_refer': '',
            'new_uv': '1',
            'als': '0',
            '_ga': 'GA1.2.576226070.1704782154',
            '_gid': 'GA1.2.1221734514.1704782154',
            '_ga_DYBJHZFBX2': 'GS1.2.1704782154.1.0.1704782154.0.0.0',
            'ajk_member_id': '251782391',
            'fzq_js_anjuke_xiaoqu_pc': 'b58d4821fe3c7efa23924704a9d28218_1704782460670_23',
            'ajkAuthTicket': 'TT=eadcc4caf4a62d9763ad5f35f6243bbc&TS=1704782457563&PBODY=R7I_W7aNyZJebCNApdksAHVy2IQRu1fg6jRH7V4zgu3IzENM4AVPdIJga1bYq80NjHYCGtYq9mL8d3xKrkV4eNQ2hYqoqrRHIVsOVGv7wk2mKN219NqwcT1Dwf4jVEQYNh_5sHCTONU1lQX8YCrI8SRRM607eMXxGQozUCLQ3k0&VER=2&CUID=39e7aRMHy6DDsOmgAVryN-JpTd-Z8guI',
            'xxzl_cid': '5ac0f643503149f1a01135e6b55d55c6',
            'xxzl_deviceid': 'Ew780eRQKV4UCwJmW767KOiJMYWYbBcp41sFTHbQDhtbS2MuEz0hI6SK9c6STvg6',
        }
        self.headers = {
            'authority': 'cs.anjuke.com',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'cache-control': 'no-cache',
            # 'cookie': 'SECKEY_ABVK=yN6sMd5ARVvSUB7X+vkXdWvn5eBpPhXdn13hkWGVZ/0%3D; BMAP_SECKEY=MXqX4OT-tHaH65jB1haMsbqW-qgBjmhRBGAY4WPuNkO3j4clIyRZde0H_7fzB-3tx98O28elBQiFIVd3E_-gHdB7123inHZzisq8NDx5Ps2ybuiHSrn56rQmrChwOabBO6WF8qlnruS9zHqTO2p5RvEZK9-bxK0ZitgaikWZ7PcAfmhYlPGHsUs4iG59AERB; aQQ_ajkguid=257EB14C-D5FC-4A0C-AE4C-B29EE0023134; sessid=ED113258-2FF4-4CD7-B0E7-E168D210D662; ajk-appVersion=; ctid=27; id58=CrIcp2WZmzZrrx8uRURFAg==; isp=true; xxzlcid=5ac0f643503149f1a01135e6b55d55c6; xxzl-cid=5ac0f643503149f1a01135e6b55d55c6; fzq_h=cd7ce9745e16db1bc94687924442373c_1704737951984_95d6e2b691194383a49fadab5eb18bdf_3740786969; twe=2; fzq_js_anjuke_ershoufang_pc=f39cf68ef60c18fcb3f2cf75b2d55029_1704781910567_25; obtain_by=2; ajk_member_verify=nGamCrFRDk0vJp%2FMiRoVQPDhLMdc%2FwFJFQTH9x1D0A8%3D; ajk_member_verify2=MjUxNzgyMzkxfEw3YlJ1Rkd8MQ%3D%3D; 58tj_uuid=86ff780a-1d02-4f6c-a3cd-f9f096505666; new_session=1; init_refer=; new_uv=1; als=0; _ga=GA1.2.576226070.1704782154; _gid=GA1.2.1221734514.1704782154; _ga_DYBJHZFBX2=GS1.2.1704782154.1.0.1704782154.0.0.0; ajk_member_id=251782391; fzq_js_anjuke_xiaoqu_pc=b58d4821fe3c7efa23924704a9d28218_1704782460670_23; ajkAuthTicket=TT=eadcc4caf4a62d9763ad5f35f6243bbc&TS=1704782457563&PBODY=R7I_W7aNyZJebCNApdksAHVy2IQRu1fg6jRH7V4zgu3IzENM4AVPdIJga1bYq80NjHYCGtYq9mL8d3xKrkV4eNQ2hYqoqrRHIVsOVGv7wk2mKN219NqwcT1Dwf4jVEQYNh_5sHCTONU1lQX8YCrI8SRRM607eMXxGQozUCLQ3k0&VER=2&CUID=39e7aRMHy6DDsOmgAVryN-JpTd-Z8guI; xxzl_cid=5ac0f643503149f1a01135e6b55d55c6; xxzl_deviceid=Ew780eRQKV4UCwJmW767KOiJMYWYbBcp41sFTHbQDhtbS2MuEz0hI6SK9c6STvg6',
            'pragma': 'no-cache',
            'referer': 'https://cs.anjuke.com/community/p1/',
            'sec-ch-ua': '"Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
        }
        self.data = {}

    def get_html(self):
        try:
            time.sleep(1)
            response = requests.get(self.url, headers=self.headers, cookies=self.cookies)
            return response.text
        except Exception as e:
            print(e)

    def parse_html(self, html):
        selector = Selector(text=html)
        try:
            # 房源名称
            house_name = selector.css('h1::text').get()
            # 房源售价
            house_price_compare = selector.css('div.house-price_compare span::text').getall()
            house_price_title = selector.css('p.house-price_title::text').get()
            house_price = " ".join(house_price_compare + [house_price_title])
            # 房源信息
            house_info = selector.xpath('//div[@class="info-list"]/div')

            for house in house_info:
                label = house.css('div.label::text').get().strip()
                hover = house.css('div.hover div.hover-inner div.hover-value::text').get()
                if hover is None:
                    hover = house.css('div.value::text').get().strip()
                else:
                    hover = hover.strip()

                self.save_data(label, hover, house_name)
        except Exception as e:
            pass

    def save_data(self, label, hover, house_name):
        self.data[label] = hover
        self.data['楼盘名称'] = house_name

    def run(self):
        html = self.get_html()
        self.parse_html(html)
        print(self.data)
        self.csv_writer.writerow(self.data)


def main():
    global COUNT
    # row_url = 'https://cs.anjuke.com/community/view/786141'
    fieldnames = [
        '楼盘名称', '物业类型', '权属类别', '竣工时间', '产权年限', '总户数',
        '总建面积', '容积率', '绿化率', '建筑类型', '所属商圈',
        '统一供暖', '供水供电', '停车位', '物业费', '停车费',
        '车位管理费', '物业公司', '小区地址', '开发商',
    ]
    with open('secondhand_house.csv', 'a+', encoding='utf-8-sig', newline='') as f:
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        csv_writer.writeheader()

    for url in main1():
        print('当前是第几个房 = ', COUNT, end='\n\n')
        COUNT = COUNT + 1
        f = open('secondhand_house.csv', 'a+', encoding='utf-8-sig', newline='')
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        spider = GetHouseDetail(url, csv_writer)
        spider.run()
        f.close()


if __name__ == '__main__':
    main()
